﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*-、基于索引的泛型集合List<T>
1..基于索引的泛型集合(Generic)
泛型是编程语言的一种风格，要求程序员使用的时候，需要明确“特定的类型”，才能实例化对象
T:表示数据类型
    比如：LIst[string]
也就是当你使用的时候，请你告诉“我”你要在这个集合中存放的数据是什么类型

2.对比数组
int[] array = new int[5]
[0] 12
[1] 23
[2] 45
[3] 56
[4] 90

特点：无法在继续增加新的数据，在扩展上有局限性 如果你定义多了，浪费空间

3.泛型集合LIST<T>
[0] <T>
[1] <T>
[2] <T>
[3] <T>
[4] <T>
。。。。。
特点：定义后，无需指定长度，可以不断增加。空间也不会浪费。
重要特点：集合List<T>作为对象的容器，用的非常多

泛型集合有强制性的要求，必须添加的元素类型完全一样


二：基于"键-值"对的泛型集合
1.Dictionry<k,v>又称为"字典集合"
k => key 键
v => value 值

2.数据存储结构
[key] <value>
[key] <value>
[key] <value>
[key] <value>
[key] <value>
*/

namespace CSharpGeneric
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Test1();
            //Test3();
            //Test4();
            Test5();
            Console.ReadLine();
        }
        static void Test1()
        {
            string[] nameList = { "常老师", "付老师" };
            string[] nameList2 = new string[5];
            nameList2[0] = "常老师";
            nameList2[1] = "付老师";

            //创建集合对象
            List<string> teacherList = new List<string>();
            teacherList.Add("刘老师");
            teacherList.Add("张老师");
            teacherList.Add("韩老师");

            //teacherList.Add(100); //

            Test2(teacherList);
        }
        //泛型集合List遍历的实现
        static void Test2(List<string> list)
        {
            foreach (string item in list)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("--------------");
            for(int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);
            }
        }
        //泛型集合List常用方法汇总
        static void Test3()
        {
            //集合初始化器
            List<string> teacherList = new List<string>
            {
                "常老师","付老师","张老师","刘老师"
            };
            //集合复制
            List<string> newTeacherList = new List<string>(teacherList);
            //List<string> list = teacherList; //
            
            //获取集合的长度(元素的个数)
            int length = teacherList.Count;
            Console.WriteLine("当前集合元素的个数"+length);

            //集合到数组的转换
            string[] teacherArray = teacherList.ToArray();
            //数组到集合的转换
            List<string> tList = teacherArray.ToList();

            //将数组可以直接添加给集合
            teacherList.Add("韩老师");
            string[] teachers = { "周老师", "赵老师" };
            teacherList.AddRange(teachers);
            int length2 = teacherList.Count;
            Console.WriteLine( $"当前集合元素的个数:{length2}");

            //判断集合中是否存在某个元素
            bool isExisted = teacherList.Contains("常老师");
            bool isExisted2 = teacherList.Contains("秦老师");
            Console.WriteLine(isExisted);
            Console.WriteLine(isExisted2);

            //在指定位置插入元素
            teacherList.Insert(2, "Annie老师");

            //移除元素
            teacherList.Remove("Annie老师"); //按照元素移除
            teacherList.RemoveAt(1); //按照索引删除
            teacherList.Clear(); //全部删除
        }
        /// <summary>
        /// 测试集合封装对象(集合其实就是对象的容器)
        /// </summary>
        public static void Test4()
        {
            Course course1 = new Course(10001,".NET/c#1","新阁教育1" ,"常老师1");
            Course course2 = new Course(10002, ".NET/c#2", "新阁教育2", "常老师2");
            Course course3 = new Course(10003, ".NET/c#3", "新阁教育3", "常老师3");
            List<Course> courseList = new List<Course>() { course1,course2,course3};
            //遍历
            foreach(Course item in courseList)
            {
                Console.WriteLine($"{item.CourseId}\t{item.CourseName}\n{item.Teacher}\t{item.Company}");
            }
        }
        static void Test5()
        {
            Teacher teacher1 = new Teacher { TeacherId = 1001, TeacherName = "常老师", Company = "新阁教育" };
            Teacher teacher2 = new Teacher { TeacherId = 1001, TeacherName = "常老师", Company = "新阁教育" };
            Teacher teacher3 = new Teacher { TeacherId = 1001, TeacherName = "常老师", Company = "新阁教育" };
            //创建集合对象并添加元素
            Dictionary<string,Teacher> teacherDic = new Dictionary<string,Teacher>();
            teacherDic.Add("常老师", teacher1);
            teacherDic.Add("付老师", teacher2);
            teacherDic.Add("王老师", teacher3);

            //集合初始化器
            Dictionary<string, Teacher> teacherDic2 = new Dictionary<string, Teacher>()
            {
                ["常老师"] = teacher1,
                ["付老师"] = teacher2,
                ["王老师"] = teacher3
            };

            //根据key获取value
            Console.WriteLine(teacherDic2["付老师"].Company);
            Console.WriteLine(teacherDic2["常老师"].WorkingYears);
            Console.WriteLine("-------------");
            //遍历
            foreach(string item in teacherDic2.Keys)
            {
                Console.WriteLine(item);
            }
            foreach (Teacher item in teacherDic2.Values)
            {
                Console.WriteLine($"{item.TeacherId}\t{item.TeacherName}\t{item.Company}");
            }
        }
    }
}
